package com.everflourish.yeah100.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.everflourish.yeah100.R;
import com.everflourish.yeah100.entity.marking.TeacherModel;

import java.util.List;

/**
 * 作者：Created by liqisi on 2016/7/4.
 * 时间：2016/7/4
 */
public class TeacherSearchAdapter extends RecyclerView.Adapter<TeacherSearchAdapter.TeacherSearchViewHolder> {

    private LayoutInflater mInflater;
    private List<TeacherModel> models;
    private Context mContext;
    private RequestQueue mQueue;

    public TeacherSearchAdapter(Context context, List<TeacherModel> models, RequestQueue mQueue) {
        mInflater = LayoutInflater.from(context);
        this.models = models;
        mContext = context;
        this.mQueue = mQueue;
    }

    @Override
    public TeacherSearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_teacher_share, parent, false);
        TeacherSearchViewHolder viewHolder = new TeacherSearchViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(TeacherSearchViewHolder holder, int position) {
        TeacherModel teacherModel = models.get(position);
        if (teacherModel.isCheck()) {
            holder.checkBox.setChecked(teacherModel.isCheck());
            holder.checkBox.setClickable(false);
        }
        holder.userNameTv.setText(teacherModel.getTeacher().getUserName());
        if (teacherModel.isCheck()) {
            holder.checkBox.setClickable(false);
        } else {
            holder.checkBox.setOnClickListener(new MyOnclickListener(holder, position, teacherModel));
        }
        holder.deleteLl.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return models.size();
    }

    class MyOnclickListener implements View.OnClickListener {

        private TeacherSearchViewHolder viewHolder;
        private int position;
        private TeacherModel teacherModel;

        public MyOnclickListener(TeacherSearchViewHolder viewHolder, int position, TeacherModel teacherModel) {
            this.viewHolder = viewHolder;
            this.position = position;
            this.teacherModel = teacherModel;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.teacher_select_ll:
                    break;
                case R.id.teacher_check_rb:
                    teacherModel.setIsCheck(viewHolder.checkBox.isChecked());
                    break;
                default:
                    break;
            }
        }
    }

    public class TeacherSearchViewHolder extends RecyclerView.ViewHolder {

        private CheckBox checkBox;
        private TextView userNameTv;
        private LinearLayout deleteLl;

        public TeacherSearchViewHolder(View itemView) {
            super(itemView);
            checkBox = (CheckBox) itemView.findViewById(R.id.teacher_check_rb);
            userNameTv = (TextView) itemView.findViewById(R.id.teacher_name_tv);
            deleteLl = (LinearLayout) itemView.findViewById(R.id.teacher_delete_ll);
        }
    }
}
